using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Transactions;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.ConnectionPool;
using OracleInternal.MTS;
using OracleInternal.NetCore;
using OracleInternal.ServiceObjects;

namespace OracleInternal.Common
{
	internal static class Trace
	{
		private static object s_syncObj;

		internal static TextWriterTraceListener s_singleTextListener;

		internal static string s_namedSlot;

		internal static string s_dateTimeFormat;

		internal static string s_dateFormat;

		internal static string s_timeFormat;

		internal static string s_oldTraceFileLocation;

		internal static string s_lastEventLogEntryAdded;

		internal static int s_oldTraceLevel;

		internal static string s_odpTraceDir;

		internal static string s_eventLogSource;

		internal static string s_eventLogName;

		internal static bool m_bEventLogEntryAdded;

		internal static long s_singleTraceFileSize;

		internal static string s_singleTraceFilePath;

		static Trace()
		{
			s_syncObj = new object();
			s_singleTextListener = null;
			s_namedSlot = "OracleInternalThreadData";
			s_dateTimeFormat = "yyyy-MM-dd HH:mm:ss.ffffff";
			s_dateFormat = "yyyy_MM_dd";
			s_timeFormat = "HH_mm_ss_ffffff";
			s_oldTraceFileLocation = null;
			s_lastEventLogEntryAdded = null;
			s_oldTraceLevel = 0;
			s_odpTraceDir = Path.Combine("ODP.NET", "core", "trace");
			s_eventLogSource = "Oracle Data Provider for .NET Core";
			s_eventLogName = "Application";
			m_bEventLogEntryAdded = false;
			s_singleTraceFileSize = 0L;
			s_singleTraceFilePath = null;
		}

		internal static void Init()
		{
			if (ConfigBaseClass.m_TraceLevel > 0)
			{
				ConfigBaseClass.m_TraceLevel |= 268435456;
				ProviderConfig.m_bTraceLevelPublic = (ConfigBaseClass.m_TraceLevel & 1) != 0;
				ProviderConfig.m_bTraceLevelPrivate = (ConfigBaseClass.m_TraceLevel & 2) != 0;
				ProviderConfig.m_bTraceLevelNetwork = (ConfigBaseClass.m_TraceLevel & 4) != 0;
				ProviderConfig.m_bTraceLevelConfig = (ConfigBaseClass.m_TraceLevel & 0x10000000) != 0;
				ProviderConfig.m_bTraceLevelSecure = (ConfigBaseClass.m_TraceLevel & 8) != 0;
				CreateEventLogSource();
			}
		}

		internal static void ReInit(bool doFinalize = true)
		{
			try
			{
				if (doFinalize)
				{
					if (s_singleTextListener != null)
					{
						lock (s_syncObj)
						{
							if (s_singleTextListener != null)
							{
								FinalizeSingleTraceListener();
							}
						}
					}
					ThreadData.RegenerateTimeStampHash();
				}
				Init();
			}
			catch
			{
			}
		}

		private static void FinalizeMultiTraceListener()
		{
			try
			{
				((ThreadData)Thread.GetData(Thread.GetNamedDataSlot(s_namedSlot)))?.Dispose();
			}
			catch
			{
			}
			finally
			{
				Thread.SetData(Thread.GetNamedDataSlot(s_namedSlot), null);
			}
		}

		private static ThreadData InitializeMultiTraceListener(bool reInit = false)
		{
			if (reInit)
			{
				FinalizeMultiTraceListener();
			}
			string traceFilePath = null;
			long traceFileSize = 0L;
			ThreadData threadData = null;
			Stream fileName = GetFileName(ConfigBaseClass.m_traceFileLocation, Thread.CurrentThread.ManagedThreadId, out traceFilePath, out traceFileSize);
			if (fileName != null)
			{
				TextWriterTraceListener textListener = new TextWriterTraceListener(fileName);
				threadData = new ThreadData
				{
					traceFile = fileName,
					textListener = textListener,
					traceFilePath = traceFilePath,
					traceFileSize = traceFileSize
				};
				Thread.SetData(Thread.GetNamedDataSlot(s_namedSlot), threadData);
			}
			return threadData;
		}

		[SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
		private static void FinalizeSingleTraceListener()
		{
			try
			{
				if (s_singleTextListener != null)
				{
					System.Diagnostics.Trace.Listeners.Remove(s_singleTextListener);
					s_singleTextListener.Dispose();
				}
			}
			catch
			{
			}
			finally
			{
				s_singleTextListener = null;
				s_singleTraceFilePath = null;
				s_singleTraceFileSize = 0L;
			}
		}

		[SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
		private static void InitializeSingleTraceListener()
		{
			Stream fileName = GetFileName(ConfigBaseClass.m_traceFileLocation, -1, out s_singleTraceFilePath, out s_singleTraceFileSize);
			if (fileName != null)
			{
				s_singleTextListener = new TextWriterTraceListener(fileName);
				System.Diagnostics.Trace.Listeners.Add(s_singleTextListener);
			}
		}

		internal static void Write(OracleTraceLevel traceLevel, OracleTraceTag traceTag, OracleTraceClassName className, OracleTraceFuncName funcName, params string[] args)
		{
			try
			{
				bool flag = false;
				if (ConfigBaseClass.m_TraceLevel <= 0 || ((uint)traceLevel & (uint)ConfigBaseClass.m_TraceLevel) != (uint)traceLevel)
				{
					return;
				}
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append(GetTimeInfo());
				string str = string.Empty;
				string str2 = string.Empty;
				string text = string.Empty;
				switch (traceLevel)
				{
				case OracleTraceLevel.Config:
					str = "(CFG)";
					break;
				case OracleTraceLevel.Public:
					str = "(PUB)";
					break;
				case OracleTraceLevel.Private:
					str = "(PRI)";
					break;
				case OracleTraceLevel.Network:
					str = "(NET)";
					break;
				}
				if (OracleTraceTag.Error == (traceTag & OracleTraceTag.Error))
				{
					traceTag &= (OracleTraceTag)(-268435457);
					text = " (ERR)";
					flag = true;
				}
				else if (OracleTraceTag.Entry == (traceTag & OracleTraceTag.Entry))
				{
					traceTag &= (OracleTraceTag)(-257);
					text = " (ENT)";
					flag = true;
				}
				else if (OracleTraceTag.Exit == (traceTag & OracleTraceTag.Exit))
				{
					traceTag &= (OracleTraceTag)(-513);
					text = " (EXT)";
					flag = true;
				}
				if (OracleTraceTag.MTS == (traceTag & OracleTraceTag.MTS))
				{
					traceTag &= (OracleTraceTag)(-4097);
					text += " (MTS)";
					flag = true;
				}
				if (OracleTraceTag.CP == (traceTag & OracleTraceTag.CP))
				{
					traceTag &= (OracleTraceTag)(-2049);
					text += " (CP)";
					flag = true;
				}
				switch (traceTag)
				{
				case OracleTraceTag.SvcObj:
					str2 = " (SVC)";
					break;
				case OracleTraceTag.TTC:
					str2 = " (TTC)";
					break;
				case OracleTraceTag.SQL:
					str2 = " (SQL)";
					break;
				case OracleTraceTag.Receive:
					str2 = " (REC)";
					break;
				case OracleTraceTag.BulkCopy:
					str2 = " (SND)";
					break;
				case OracleTraceTag.REFCursor:
					str2 = " (REF)";
					break;
				case OracleTraceTag.EDM:
					str2 = " (EDM)";
					break;
				case OracleTraceTag.Config:
					switch (className)
					{
					case OracleTraceClassName.OracleConfiguration:
					case OracleTraceClassName.OracleDataSourceCollection:
					case OracleTraceClassName.OracleOnsServerCollection:
						flag = true;
						str2 = " (OCFG)";
						break;
					case OracleTraceClassName.JsonConfigFileSettings:
						str2 = " (JSON)";
						break;
					default:
						str2 = " (.NET)";
						break;
					}
					break;
				case OracleTraceTag.Environment:
					str2 = " (ENV)";
					break;
				case OracleTraceTag.Sqlnet:
					str2 = " (SQLNET)";
					break;
				case OracleTraceTag.BinXML:
					str2 = " (BINXML)";
					break;
				case OracleTraceTag.Tnsnames:
					str2 = " (TNSNAMES)";
					break;
				case OracleTraceTag.Version:
					str2 = " (VER)";
					break;
				case OracleTraceTag.SelfTuning:
					str2 = " (TUN)";
					break;
				case OracleTraceTag.RLB:
					str2 = " (RLB)";
					break;
				case OracleTraceTag.HA:
					str2 = " (HA)";
					break;
				case OracleTraceTag.ONS:
					str2 = " (ONS)";
					break;
				case OracleTraceTag.BUF:
					str2 = " (BUF)";
					break;
				case OracleTraceTag.Prm:
					str2 = " (PRM)";
					break;
				case OracleTraceTag.OCSSYNC:
					str2 = " (OCSSYNC)";
					break;
				case OracleTraceTag.TTIRPA:
					str2 = " (TTIRPA)";
					break;
				case OracleTraceTag.Sharding:
					str2 = " (SHARD)";
					break;
				}
				if (traceLevel == OracleTraceLevel.Config)
				{
					stringBuilder.AppendFormat("{0,-17}", str + str2 + text);
				}
				else
				{
					stringBuilder.AppendFormat("{0,-10} ", str + str2 + text);
				}
				if (flag)
				{
					if (funcName.ToString() == "cctor")
					{
						stringBuilder.Append(className.ToString() + ".." + funcName.ToString() + "()");
					}
					else
					{
						stringBuilder.Append(className.ToString() + "." + funcName.ToString() + "()");
					}
				}
				if (args.Length != 0)
				{
					if (flag)
					{
						stringBuilder.Append(" ");
					}
					string[] array = new string[args.Length];
					Array.Copy(args, 1, array, 0, args.Length - 1);
					if (array != null)
					{
						string format = args[0];
						object[] args2 = array;
						stringBuilder.AppendFormat(format, args2);
					}
				}
				stringBuilder.Append("\r\n");
				WriteTrace(stringBuilder.ToString());
			}
			catch
			{
			}
		}

		internal static void Write(OracleTraceLevel traceLevel, OracleTraceTag tracetag, byte[] dataBuffer, int offset, int size)
		{
			try
			{
				if (ConfigBaseClass.m_TraceLevel <= 0 || ((uint)traceLevel & (uint)ConfigBaseClass.m_TraceLevel) != (uint)traceLevel)
				{
					return;
				}
				StringBuilder stringBuilder = new StringBuilder();
				string asciiCharacters = GetAsciiCharacters(dataBuffer, offset, size);
				int num = 0;
				int num2 = 8;
				int num3 = 0;
				_ = DateTime.Now;
				while (size > 0)
				{
					num3 = Math.Min(size, num2);
					string text = BitConverter.ToString(dataBuffer, offset + num2 * num, num3);
					string arg = asciiCharacters.Substring(num2 * num, num3);
					num++;
					size -= num2;
					stringBuilder.AppendFormat(GetTimeInfo());
					switch (tracetag)
					{
					case OracleTraceTag.BulkCopy:
						stringBuilder.AppendFormat("{0,-12}", "(NET) (SND)");
						break;
					case OracleTraceTag.Receive:
						stringBuilder.AppendFormat("{0,-12}", "(NET) (REC)");
						break;
					}
					stringBuilder.AppendFormat("{0,-26}|{1,-8}|", text.Replace("-", " "), arg);
					stringBuilder.Append("\r\n");
				}
				WriteTrace(stringBuilder.ToString());
			}
			catch
			{
			}
		}

		private static string GetTimeInfo()
		{
			return string.Format("{0} TID:{1,-3} ", DateTime.Now.ToString(s_dateTimeFormat), Thread.CurrentThread.ManagedThreadId);
		}

		private static void WriteTrace(string message)
		{
			if (!TracingState.s_bTracingStarted)
			{
				return;
			}
			if (ConfigBaseClass.m_TraceOption == 0)
			{
				lock (s_syncObj)
				{
					if ((s_singleTextListener == null || s_oldTraceFileLocation != ConfigBaseClass.m_traceFileLocation || s_oldTraceLevel != ConfigBaseClass.m_TraceLevel || s_singleTraceFileSize > ConfigBaseClass.m_TraceFileMaxSize) && ConfigBaseClass.m_TraceLevel > 0)
					{
						FinalizeSingleTraceListener();
						InitializeSingleTraceListener();
						s_oldTraceLevel = ConfigBaseClass.m_TraceLevel;
						s_oldTraceFileLocation = ConfigBaseClass.m_traceFileLocation;
						ProviderConfig.TraceConfigAndEnvParams();
						OracleConfiguration.DumpInfo();
					}
					s_singleTraceFileSize += message.Length;
					s_singleTextListener.Write(message);
					s_singleTextListener.Flush();
				}
				return;
			}
			ThreadData threadData = (ThreadData)Thread.GetData(Thread.GetNamedDataSlot(s_namedSlot));
			if ((threadData == null || s_oldTraceFileLocation != ConfigBaseClass.m_traceFileLocation || s_oldTraceLevel != ConfigBaseClass.m_TraceLevel || threadData.traceFileSize > ConfigBaseClass.m_TraceFileMaxSize) && ConfigBaseClass.m_TraceLevel > 0)
			{
				FinalizeMultiTraceListener();
				threadData = InitializeMultiTraceListener();
				s_oldTraceFileLocation = ConfigBaseClass.m_traceFileLocation;
				s_oldTraceLevel = ConfigBaseClass.m_TraceLevel;
				ProviderConfig.TraceConfigAndEnvParams();
				OracleConfiguration.DumpInfo();
			}
			if (threadData != null)
			{
				if (threadData.IsOutdated)
				{
					threadData = InitializeMultiTraceListener(reInit: true);
					ProviderConfig.TraceConfigAndEnvParams();
					OracleConfiguration.DumpInfo();
				}
				threadData.traceFileSize += message.Length;
				threadData.textListener.Write(message);
				threadData.textListener.Flush();
			}
		}

		[SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
		private static Stream OpenFile(string singleTraceFileLocation, string traceFileName, int threadId, out string traceFilePath, out long traceFileSize)
		{
			Stream result = null;
			traceFilePath = null;
			traceFileSize = 0L;
			try
			{
				string text = null;
				singleTraceFileLocation = (ConfigBaseClass.m_singleTraceFileLocation = ConfigBaseClass.GetResolvedFileLocation(singleTraceFileLocation));
				if (!Directory.Exists(singleTraceFileLocation))
				{
					return null;
				}
				string text2 = (traceFilePath = singleTraceFileLocation + Path.DirectorySeparatorChar + traceFileName);
				bool flag = false;
				try
				{
					result = File.Open(text2, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
					FileInfo fileInfo = new FileInfo(text2);
					traceFileSize = fileInfo.Length;
					flag = true;
				}
				catch
				{
					flag = false;
					result = null;
				}
				try
				{
					if (text != null)
					{
						NetCoreEventLog.WriteEntry(s_eventLogSource, s_eventLogSource + " has detected that " + text + " is not a valid/write-able directory.");
					}
					if (flag)
					{
						if (s_lastEventLogEntryAdded != singleTraceFileLocation)
						{
							lock (s_syncObj)
							{
								if (!(s_lastEventLogEntryAdded != singleTraceFileLocation))
								{
									return result;
								}
								if (ConfigBaseClass.m_TraceOption == 0)
								{
									NetCoreEventLog.WriteEntry(s_eventLogSource, s_eventLogSource + " has opened trace file " + text2 + ".");
								}
								else
								{
									NetCoreEventLog.WriteEntry(s_eventLogSource, s_eventLogSource + " has opened trace file " + singleTraceFileLocation + Path.DirectorySeparatorChar + ConfigBaseClass.ApplicationName.ToUpperInvariant() + "." + ConfigBaseClass.CoreRunTimeExt.ToUpperInvariant() + "_PID_" + ConfigBaseClass.CurrentProcess.Id + "_<TID>_<DATE>_<TIME>.trc.");
								}
								s_lastEventLogEntryAdded = singleTraceFileLocation;
								return result;
							}
						}
						return result;
					}
					NetCoreEventLog.WriteEntry(s_eventLogSource, s_eventLogSource + " could not open trace file(s) at " + singleTraceFileLocation + ".");
					return result;
				}
				catch
				{
					return result;
				}
			}
			catch
			{
				return result;
			}
		}

		private static void CreateEventLogSource()
		{
			try
			{
				if (!NetCoreEventLog.SourceExists(s_eventLogSource))
				{
					NetCoreEventLog.CreateEventSource(s_eventLogSource, s_eventLogName);
				}
			}
			catch
			{
			}
		}

		[SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
		private static Stream GetFileName(string singleTraceFileLocation, int threadId, out string traceFilePath, out long traceFileSize)
		{
			DateTime now = DateTime.Now;
			string text = null;
			_ = string.Empty;
			Stream stream = null;
			traceFilePath = null;
			traceFileSize = 0L;
			string text2 = "";
			if (threadId != -1)
			{
				text2 = "_TID_" + threadId;
			}
			text = ConfigBaseClass.ApplicationName.ToUpperInvariant() + "." + ConfigBaseClass.CoreRunTimeExt.ToUpperInvariant() + "_PID_" + ConfigBaseClass.CurrentProcess.Id + text2 + "_DATE_" + now.ToString(s_dateFormat) + "_TIME_" + now.ToString(s_timeFormat) + ".trc";
			if (singleTraceFileLocation != null && singleTraceFileLocation != string.Empty)
			{
				stream = OpenFile(singleTraceFileLocation, text, threadId, out traceFilePath, out traceFileSize);
			}
			if (stream == null)
			{
				try
				{
					singleTraceFileLocation = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), s_odpTraceDir)).FullName;
				}
				catch
				{
				}
				stream = OpenFile(singleTraceFileLocation, text, threadId, out traceFilePath, out traceFileSize);
			}
			return stream;
		}

		private static string GetAsciiCharacters(byte[] dataBuffer, int offset, int size)
		{
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = offset; i < offset + size; i++)
			{
				byte b = dataBuffer[i];
				if (b > 31 && b < 127)
				{
					stringBuilder.Append((char)b);
				}
				else
				{
					stringBuilder.Append(".");
				}
			}
			return stringBuilder.ToString();
		}

		public static string GetCPInfo(OracleConnectionImpl con, object transaction, string affinityInstance, string oper, bool bResourcePoolInfo = false, bool bDistributionInfo = false, string[] instanceNames = null)
		{
			string text = null;
			string text2 = null;
			string text3 = null;
			StringBuilder stringBuilder = new StringBuilder();
			string text4 = null;
			TransactionContext<OraclePoolManager, OraclePool, OracleConnectionImpl> transactionContext = null;
			try
			{
				if (con != null && con.m_localTxnId != null && con.m_localTxnId != string.Empty)
				{
					text2 = con.m_localTxnId;
				}
				else if (transaction != null)
				{
					if (transaction is Transaction)
					{
						text2 = ((Transaction)transaction).TransactionInformation.LocalIdentifier;
					}
					else if (transaction is string)
					{
						text2 = (string)transaction;
					}
				}
				else if (Transaction.Current != null)
				{
					text2 = Transaction.Current.TransactionInformation.LocalIdentifier;
				}
				if (con == null)
				{
					stringBuilder.Append("(txnid=");
					if (text2 != null && text2 != string.Empty)
					{
						stringBuilder.Append(text2);
					}
					else
					{
						stringBuilder.Append("n/a");
					}
					stringBuilder.Append(") ");
					if (text2 != null)
					{
						stringBuilder.Append(OracleConnection.Dump(text2));
					}
					return stringBuilder.ToString();
				}
				OraclePoolManager pm = con.m_pm;
				if (con.m_txnCtx != null)
				{
					try
					{
						transactionContext = con.m_txnCtx;
					}
					catch
					{
					}
					try
					{
						text = con.m_txnCtx.m_localTxnId;
					}
					catch
					{
					}
					try
					{
						text3 = con.m_txnCtx.m_affinityInstanceName;
					}
					catch
					{
					}
					try
					{
						if (con.m_txnCtx.m_mtsTxnRM != null)
						{
							text4 = con.m_txnCtx.m_mtsTxnRM.m_RMGuid.ToString();
						}
					}
					catch
					{
					}
				}
				if (affinityInstance == null || affinityInstance == string.Empty)
				{
					affinityInstance = text3;
				}
				if (oper != null && oper != string.Empty)
				{
					stringBuilder.Append("(oper=");
					stringBuilder.Append(oper);
					stringBuilder.Append(") ");
				}
				stringBuilder.Append("(aff=");
				stringBuilder.Append((affinityInstance == null || affinityInstance == string.Empty) ? "n/a" : affinityInstance);
				stringBuilder.Append(") (inst=");
				stringBuilder.Append(con.m_instanceName);
				stringBuilder.Append(") (affmatch=");
				stringBuilder.Append((affinityInstance == null || affinityInstance == string.Empty) ? "n/a" : (affinityInstance == con.m_instanceName).ToString().Substring(0, 1));
				stringBuilder.Append(") (pr.service=");
				stringBuilder.Append(con.ServiceName);
				stringBuilder.Append(") (pr.pdb=");
				stringBuilder.Append(con.PdbName);
				stringBuilder.Append(") (pr.edition=");
				stringBuilder.Append(con.EditionName);
				stringBuilder.Append(") (sessid=");
				stringBuilder.Append(con.m_endUserSessionId);
				stringBuilder.Append(":");
				stringBuilder.Append(con.m_endUserSerialNum);
				stringBuilder.Append(") ");
				if (con.m_pxyUserSessionId != -1)
				{
					stringBuilder.Append(") (psessid=");
					stringBuilder.Append(con.m_pxyUserSessionId);
					stringBuilder.Append(":");
					stringBuilder.Append(con.m_pxyUserSerialNum);
					stringBuilder.Append(") ");
				}
				if (con != null)
				{
					stringBuilder.Append(string.Format("({0};{1};{2};{3};{4}) ", con.m_bCheckedOutByApp.ToString().Substring(0, 1), con.m_bCheckedOutByDTC.ToString().Substring(0, 1), con.m_bPutCompleted.ToString().Substring(0, 1), con.m_instanceName, (con.m_mtsTxnCtx != null) ? con.m_mtsTxnCtx.m_txnType.ToString().Substring(0, 1) : "N"));
				}
				if (pm != null)
				{
					stringBuilder.Append("(pmid=");
					stringBuilder.Append(pm.m_id);
					stringBuilder.Append(") ");
				}
				if ((Transaction.Current != null && con.m_cs != null && con.m_cs.m_enlist == Enlist.True) || text2 != null)
				{
					MTSTxnCtx mtsTxnCtx = con.m_mtsTxnCtx;
					MTSTxnBranch mTSTxnBranch = null;
					if (mtsTxnCtx != null)
					{
						try
						{
							mTSTxnBranch = con.m_mtsTxnCtx.m_mtsTxnBranch;
						}
						catch
						{
						}
						stringBuilder.Append("(txntype=");
						stringBuilder.Append(mtsTxnCtx.m_txnType.ToString().Substring(0, 1));
						stringBuilder.Append(") ");
						if (mTSTxnBranch != null)
						{
							stringBuilder.Append("(br=");
							stringBuilder.Append(mTSTxnBranch.BranchNumber);
							stringBuilder.Append(") ");
						}
						else
						{
							stringBuilder.Append("(br=n/a) ");
						}
					}
					if (text4 != null)
					{
						stringBuilder.Append("(rmid=");
						stringBuilder.Append(text4);
						stringBuilder.Append(") ");
					}
					stringBuilder.Append("(txnid=");
					if (text2 != null && text2 != string.Empty)
					{
						stringBuilder.Append(text2);
					}
					else
					{
						stringBuilder.Append("n/a");
					}
					stringBuilder.Append(") ");
					if (mtsTxnCtx != null && text != null && text != string.Empty)
					{
						if (text2 != mtsTxnCtx.m_txnLocalID || text2 != text)
						{
							stringBuilder.Append("(txnidmatch=F) ");
							if (text2 != mtsTxnCtx.m_txnLocalID)
							{
								stringBuilder.Append("(mtstxnid=");
								stringBuilder.Append(mtsTxnCtx.m_txnLocalID);
								stringBuilder.Append(") ");
							}
							else if (text2 != text)
							{
								stringBuilder.Append("(mtstxnid=");
								stringBuilder.Append(mtsTxnCtx.m_txnLocalID);
								stringBuilder.Append(") ");
							}
						}
						else
						{
							stringBuilder.Append("(txnidmatch=T) ");
						}
					}
				}
				if (bResourcePoolInfo && transactionContext != null)
				{
					OracleConnectionImpl oracleConnectionImpl = null;
					lock (transactionContext)
					{
						for (int i = 0; i <= transactionContext.m_maxBranchIndex; i++)
						{
							oracleConnectionImpl = transactionContext.m_enlistedPRList[i];
							if (oracleConnectionImpl != null)
							{
								if (oracleConnectionImpl.m_sessionType != SessionType.Two_Session_Proxy)
								{
									stringBuilder.Append($"[{i}:({oracleConnectionImpl.m_endUserSessionId}:{oracleConnectionImpl.m_endUserSerialNum});{oracleConnectionImpl.m_bCheckedOutByApp.ToString().Substring(0, 1)};{oracleConnectionImpl.m_bCheckedOutByDTC.ToString().Substring(0, 1)};{oracleConnectionImpl.m_bPutCompleted.ToString().Substring(0, 1)};{oracleConnectionImpl.m_instanceName}]");
								}
								else
								{
									stringBuilder.Append($"[{i}:({oracleConnectionImpl.m_endUserSessionId}:{oracleConnectionImpl.m_endUserSerialNum})({oracleConnectionImpl.m_pxyUserSessionId}:{oracleConnectionImpl.m_pxyUserSerialNum});{oracleConnectionImpl.m_bCheckedOutByApp.ToString().Substring(0, 1)};{oracleConnectionImpl.m_bCheckedOutByDTC.ToString().Substring(0, 1)};{oracleConnectionImpl.m_bPutCompleted.ToString().Substring(0, 1)};{oracleConnectionImpl.m_instanceName}]");
								}
							}
							else
							{
								stringBuilder.Append($"[{i}:null]");
							}
						}
					}
					stringBuilder.Append(" ");
				}
				if (bDistributionInfo && pm != null)
				{
					RLB rLB = null;
					RLB rLB2 = null;
					if (con != null && !string.IsNullOrEmpty(con.ServiceName))
					{
						rLB = RLBManager.Get((con.m_databaseName + "|" + con.ServiceName).ToLowerInvariant());
						if (instanceNames != null && rLB != null)
						{
							rLB2 = rLB;
							rLB = rLB.GetSnapshot(new List<string>(instanceNames));
						}
					}
					if (rLB == null)
					{
						List<OraclePool> list = pm.m_pmListCP.GetList();
						stringBuilder.Append("(DISP) ");
						foreach (OraclePool item in list)
						{
							int count = item.m_cpQueuePR.Count;
							int count2 = item.m_cpListPR.Count;
							stringBuilder.Append("(service=");
							stringBuilder.Append(item.m_serviceName);
							stringBuilder.Append(";inst=");
							stringBuilder.Append(item.m_instanceName);
							if (item != null)
							{
								stringBuilder.Append(";used=");
								stringBuilder.Append(count2 - count);
								stringBuilder.Append(";idle=");
								stringBuilder.Append(count);
								stringBuilder.Append(";tot=");
								stringBuilder.Append(count2);
								stringBuilder.Append(") ");
							}
						}
					}
					else if (rLB != null)
					{
						List<OraclePool> list2 = pm.m_pmListCP.GetList();
						int num = 0;
						int num2 = 0;
						stringBuilder.Append("(DISP) (RLB) ");
						if (rLB != null)
						{
							if (rLB2 != null)
							{
								for (int j = 0; j < rLB.m_instances.Length; j++)
								{
									for (int k = 0; k < rLB2.m_instances.Length; k++)
									{
										if (rLB.m_instances[j] == rLB2.m_instances[k])
										{
											if (con.m_instanceName.ToLowerInvariant() == rLB2.m_instances[k].ToLowerInvariant())
											{
												Interlocked.Increment(ref rLB2.m_dispenseCounter[k]);
											}
											num += rLB2.m_dispenseCounter[k];
											rLB.m_dispenseCounter[j] = rLB2.m_dispenseCounter[k];
											break;
										}
									}
								}
							}
							else
							{
								for (int l = 0; l < rLB.m_dispenseCounter.Length; l++)
								{
									if (con.m_instanceName.ToLowerInvariant() == rLB.m_instances[l].ToLowerInvariant())
									{
										Interlocked.Increment(ref rLB.m_dispenseCounter[l]);
									}
									num += rLB.m_dispenseCounter[l];
								}
							}
							for (int m = 0; m < rLB.m_instances.Length; m++)
							{
								OraclePool oraclePool = null;
								int num3 = 0;
								int num4 = 0;
								for (int n = 0; n < list2.Count; n++)
								{
									if (list2[n].m_instanceName.ToLowerInvariant() == rLB.m_instances[m].ToLowerInvariant())
									{
										oraclePool = list2[n];
										num3 = oraclePool.m_cpQueuePR.Count;
										num4 = oraclePool.m_cpListPR.Count;
										break;
									}
								}
								stringBuilder.Append("(service=");
								stringBuilder.Append(rLB.m_service);
								stringBuilder.Append(";inst=");
								stringBuilder.Append(rLB.m_instances[m]);
								if (oraclePool != null)
								{
									stringBuilder.Append("; used=");
									stringBuilder.Append(num4 - num3);
									stringBuilder.Append("; idle=");
									stringBuilder.Append(num3);
									stringBuilder.Append("; tot=");
									stringBuilder.Append(num4);
								}
								else
								{
									stringBuilder.Append("; used=0; idle=0; tot=0");
								}
								num2 = rLB.m_dispenseCounter[m];
								stringBuilder.Append("; rlb pct=");
								stringBuilder.Append(rLB.m_rlbPercentages[m].ToString());
								stringBuilder.Append("%");
								stringBuilder.Append("; act pct=");
								stringBuilder.Append(num2);
								stringBuilder.Append("/");
								stringBuilder.Append(num);
								stringBuilder.Append(" (");
								if (num != 0)
								{
									stringBuilder.Append((int)((double)num2 / (double)num * 100.0));
									stringBuilder.Append("%)) ");
								}
								else
								{
									stringBuilder.Append("0%)) ");
								}
							}
							stringBuilder.Append("(miss=");
							stringBuilder.Append(pm.m_rlbMissCount);
							stringBuilder.Append(") ");
						}
					}
				}
				if (text2 != null)
				{
					stringBuilder.Append(OracleConnection.Dump(text2));
				}
			}
			catch
			{
			}
			return stringBuilder.ToString();
		}

		internal static string GetMTSTraceOutput(OraclePoolManager pm, OracleConnectionImpl conImpl, string txnid, string operation, string txnctxid = null, string respool = null)
		{
			StringBuilder stringBuilder = new StringBuilder();
			if (operation != null)
			{
				stringBuilder.Append("(oper=");
				stringBuilder.Append(operation);
				stringBuilder.Append(") ");
			}
			if (pm != null)
			{
				stringBuilder.Append("(pmid=");
				stringBuilder.Append(pm.m_id);
				stringBuilder.Append(") ");
			}
			else if (conImpl != null && conImpl.m_pm != null)
			{
				stringBuilder.Append("(pmid=");
				stringBuilder.Append(conImpl.m_pm.m_id);
				stringBuilder.Append(") ");
			}
			if (conImpl != null)
			{
				stringBuilder.Append("(sessid=");
				stringBuilder.Append(conImpl.m_endUserSessionId.ToString());
				stringBuilder.Append(") ");
				stringBuilder.Append("(implId=");
				stringBuilder.Append(conImpl.m_id);
				stringBuilder.Append(") ");
				stringBuilder.Append("(app=");
				stringBuilder.Append(conImpl.m_bCheckedOutByApp.ToString());
				stringBuilder.Append(") ");
				stringBuilder.Append("(dtc=");
				stringBuilder.Append(conImpl.m_bCheckedOutByDTC.ToString());
				stringBuilder.Append(") ");
				stringBuilder.Append("(put=");
				stringBuilder.Append(conImpl.m_bPutCompleted.ToString());
				stringBuilder.Append(") ");
			}
			if (conImpl != null && conImpl.m_cs != null)
			{
				stringBuilder.Append("(pooling=");
				stringBuilder.Append(conImpl.m_cs.m_pooling);
				stringBuilder.Append(") ");
			}
			if (txnid != null)
			{
				stringBuilder.Append("(MTS) (txnid=");
				stringBuilder.Append(txnid);
				stringBuilder.Append(") ");
			}
			else if (conImpl != null && conImpl.m_txnCtx != null)
			{
				stringBuilder.Append("(MTS) (txnid=");
				stringBuilder.Append(conImpl.m_txnCtx.m_localTxnId);
				stringBuilder.Append(") ");
			}
			if (txnctxid != null)
			{
				stringBuilder.Append("(txnctxid=");
				stringBuilder.Append(txnctxid);
				stringBuilder.Append(") ");
			}
			else if (conImpl != null && conImpl.m_txnCtx != null)
			{
				stringBuilder.Append("(txnctxid=");
				stringBuilder.Append(conImpl.m_txnCtx.m_id);
				stringBuilder.Append(") ");
			}
			if (conImpl != null && conImpl.m_mtsTxnCtx != null)
			{
				stringBuilder.Append("(txntype=");
				stringBuilder.Append(conImpl.m_mtsTxnCtx.m_txnType);
				stringBuilder.Append(") ");
			}
			if (conImpl != null && conImpl.m_txnCtx != null && conImpl.m_txnCtx.m_mtsTxnRM != null)
			{
				stringBuilder.Append("(rmid=");
				stringBuilder.Append(conImpl.m_txnCtx.m_mtsTxnRM.m_RMGuid.ToString());
				stringBuilder.Append(") ");
			}
			if (conImpl != null && conImpl.m_mtsTxnCtx != null && conImpl.m_mtsTxnCtx.m_mtsTxnBranch != null)
			{
				stringBuilder.Append("(br=");
				stringBuilder.Append(conImpl.m_mtsTxnCtx.m_mtsTxnBranch.BranchNumber);
				stringBuilder.Append(") ");
			}
			if (respool != null)
			{
				stringBuilder.Append("(respool=");
				stringBuilder.Append(respool);
				stringBuilder.Append(") ");
			}
			return stringBuilder.ToString();
		}
	}
}
